package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.client.OrderClient;
import com.woniu.client.dto.OrderDto;
import com.woniu.client.dto.StationDto;
import com.woniu.comment.util.StateData;
import com.woniu.exception.RebornException;
import com.woniu.mapper.InventoryMapper;
import com.woniu.model.Inventory;
import com.woniu.model.Result;
import com.woniu.service.InventoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.service.param.GetByTimeParam;
import org.checkerframework.checker.units.qual.A;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @autor 吴彦祖
 * @since 2024年05月23日
 */
@Service
@Transactional
public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory> implements InventoryService {

    @Resource
    private InventoryMapper inventoryMapper;
    @Resource
    private OrderClient orderClient;

    /**
     *根据id查找入库详情
     * @param id 订单id
     */
    @Override
    public Inventory getByOrderId(String id) {
        Result<OrderDto> byId = orderClient.findById(id);
        if (byId.getCode()!=200){
            throw new RebornException("不存在的订单",60001);
        }
        Inventory inventory = inventoryMapper.selectOne(new QueryWrapper<Inventory>().eq("order_num", id));
        if (inventory==null){
            throw new RebornException("该订单还未入库",80001);
        }
        return inventory;
    }

    /**
     * 根据站点id查询入库详情Page
     * id 站点id
     */
    @Override
    public Page<Inventory> getByStationId(Integer id,Integer pageNum,Integer pageSize) {
        pageNum = pageNum==null?1:pageNum;
        pageSize = pageSize==null?3:pageSize;
        Page<Inventory> inventoryPage = inventoryMapper.selectPage(new Page<>(pageNum, pageSize), new QueryWrapper<Inventory>().eq("station_id", id));
        return inventoryPage;
    }

    /**
     * 根据时间查询某站点入库详情
     * id 站点id
     */
    @Override
    public Page<Inventory> getByTime(GetByTimeParam param) {
        Integer id = param.getId(); // 站点id
        Integer year = param.getYear(); // 选择的年份
        Integer month = param.getMonth(); // 选择的月份
        // 获取分页参数，默认为第1页，每页3条数据
        Integer pageNum = param.getPageNum();
        Integer pageSize = param.getPageSize();
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 3 : pageSize;

        // 查询站点的所有入库详情
        QueryWrapper<Inventory> wrapper = new QueryWrapper<>();
        wrapper.eq("station_id", id);
        List<Inventory> inventories = inventoryMapper.selectList(wrapper);

        if (inventories == null || inventories.isEmpty()) {
            throw new RebornException("该站点不存在任何入库详情", 50001);
        }

        // 筛选符合指定年份和月份的入库详情
        List<Inventory> filteredInventories = inventories.stream()
                .filter(inventory -> {
                    LocalDateTime startTime = inventory.getStartTime();
                    return startTime.getYear() == year && startTime.getMonthValue() == month;
                })
                .collect(Collectors.toList());

        if (filteredInventories.isEmpty()) {
            throw new RebornException("选定时间段内没有任何入库详情", 50002);
        }

        // 手动分页
        int total = filteredInventories.size();
        int fromIndex = (pageNum - 1) * pageSize;
        int toIndex = Math.min(fromIndex + pageSize, total);

        if (fromIndex >= total) {
            throw new RebornException("页码超出范围", 50003);
        }

        List<Inventory> pageList = filteredInventories.subList(fromIndex, toIndex);

        // 构建返回的分页对象
        Page<Inventory> page = new Page<>(pageNum, pageSize, total);
        page.setRecords(pageList);

        return page;
    }
}
